package com.dearbinge.opentapi.content.transactionFlow;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.transactionFlow.TransactionFlowService;
import com.dearbinge.data.pojo.transactionFlow.TransactionFlow;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.date.DateFormatUtils;
import com.dearbinge.utils.date.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Api(tags = "c：交易流水表")
@RestController
@CrossOrigin
@RequestMapping("transactionFlow")
@EnableAutoConfiguration
public class TransactionFlowController {

    @Autowired
    private TransactionFlowService transactionFlowService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody TransactionFlow transactionFlow){
        try {
            if (transactionFlow.getId() != null && transactionFlow.getId() != 0){
                Integer dataCode = transactionFlowService.update(transactionFlow);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                Integer dataCode = transactionFlowService.insert(transactionFlow);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<TransactionFlow> getById(Integer id){
        try {
            TransactionFlow transactionFlow = transactionFlowService.getById(id);
            return ResultMsg.ok(transactionFlow);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID逻辑删除
     */
    @ApiOperation(value = "根据ID逻辑删除",notes = "根据ID逻辑删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = transactionFlowService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据条件分页查询
     */
    @ApiOperation(value = "根据条件分页查询",notes = "根据条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
            @ApiImplicitParam(name = "time",value = "筛选月份(yyyy-MM-dd,yyyy-MM-dd)",paramType = "query"),
            @ApiImplicitParam(name = "pageNo",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "页面数量",paramType = "query")
    })
    @RequestMapping(value = "/getByPage",method = RequestMethod.GET)
    public ResultMsg<Page> getByPage(String userId,String time,Integer pageNo,Integer pageSize){
        try {
            String startDate = null;
            String endDate = null;
            Date startTime = null;
            Date endTime = null;
            if (time != null && time != ""){
                String[] split = time.split(",");
                if (split != null && split.length > 1){
                    /*System.out.println("***********************"+split.length);*/
                    String str1 = split[0];
                    String str2 = split[1];
                    String[] split1 = str1.split("-");
                    String[] split2 = str2.split("-");
                    Integer m = Integer.parseInt(split1[1]);
                    Integer d = Integer.parseInt(split1[2]);
                    Integer m2 = Integer.parseInt(split2[1]);
                    Integer d2 = Integer.parseInt(split2[2]);
                    String mm = m.toString();
                    String dd = d.toString();
                    String mm2 = m2.toString();
                    String dd2 = d2.toString();
                    if (m < 10){
                        mm = "0"+ m;
                    }
                    if (d < 10){
                        dd = "0" + d;
                    }
                    if (m2 < 10){
                        mm2 = "0"+ m2;
                    }
                    if (d2 < 10){
                        dd2 = "0" + d2;
                    }
                    str1 = split1[0] + mm + dd;
                    str2 = split2[0] + mm2 + dd2;
                    startTime = DateUtils.getStartTime(DateUtils.string2Date(str1));
                    endTime = DateUtils.getEndTime(DateUtils.string2Date(str2));
                }else if(split != null){
                    String str1 = split[0];
                    startTime = DateUtils.getStartTime(DateUtils.string2Date(str1));
                    endTime = DateUtils.getEndTime(DateUtils.string2Date(str1));
                }
                startDate = DateUtils.date2String(startTime);
                endDate = DateUtils.date2String(endTime);
            }
            Page page = transactionFlowService.getByPage(startDate,endDate,userId,pageNo,pageSize);
            return ResultMsg.okData(page,"200");
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }
}
